package cc.yugu.yhdw.webserver.service.basic;


import cc.yugu.yhdw.common.constant.CacheKey;
import cc.yugu.yhdw.common.domain.basic.Passport;
import cc.yugu.yhdw.common.domain.basic.PassportInfo;
import cc.yugu.yhdw.common.tool.memcached.MemCachedConfig;
import com.whalin.MemCached.MemCachedClient;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 客户信息业务切面类
 * 
 * @Date:Created in 2018/7/4 10:28
 * @Author:
 * @Modified By: zjn 2018/7/4 10:28
 */ 
@Aspect
@Component
public class PassportServiceAspect {

    @Autowired
    MemCachedClient memCachedClient;

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.basic.PassportService.findInfo(..))")
    private void findInfo() {}
    /**
    * 查询缓存数据
    * 
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/4  11:14
    */ 
    @Around("findInfo()")
    public Object findInfo(ProceedingJoinPoint joinPoint) throws Throwable {
        Integer id = (Integer) joinPoint.getArgs()[0];
        // 替换输出
        String key = CacheKey.key(CacheKey.K_ID_V_PASSPORT_INFO, id);
        PassportInfo PassportInfo = (PassportInfo) memCachedClient.get(key);
        if(PassportInfo != null) {
            return PassportInfo;
        }

        Object result = joinPoint.proceed();
        if(result != null) {
            memCachedClient.set(key, result, MemCachedConfig.CACHE_ONE_WEEK);
        }
        return result;
    }

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.basic.PassportService.update(..))")
    private void update() {}
    /**
    * 更新缓存数据
    * 
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/4  11:22
    */ 
    @Around("update()")
    public Object update(ProceedingJoinPoint joinPoint) throws Throwable {
        Passport passport = (Passport) joinPoint.getArgs()[0];
        Object result = joinPoint.proceed();

        String key = CacheKey.key(CacheKey.K_ID_V_PASSPORT_INFO, passport.getId());
        memCachedClient.delete(key);
        return result;
    }
}
